﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="HomeController.cs" company="">
//   Copyright (c) 2010/2011 All Right Reserved
// </copyright>
// <summary>
//   Contains HomeController class declaration.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace IBupro.Controllers
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;

    using IBupro.Controllers.Classes;
    using IBupro.Models;

    using iBuproModel;
    using iBuproModel.Repositories.Classes;
    using iBuproModel.Repositories.Interfaces;

    /// <summary>
    /// This controller cotains actions related to home page.
    /// </summary>
    public class HomeController : CustomController
    {
        public ActionResult Index()
        {

            return RedirectToAction("DiplomProjects", "Search");//View( new SearchDiplomProject());   
        }

        [HttpPost]
        public ActionResult Index(SearchDiplomProject search)
        {
            if (!ModelState.IsValid)
                return View(search);

            return View(search);
        }

        public ActionResult StudentPlans()
        {
            IStudentPlanRepository repository = new StudentPlanRepository();
            return View(repository.GetAll());
        }

        public ActionResult Departments()
        {
            IDepartmentRepository repository = repositoryService.DepartmentRepositoryCreateInstance();
            return View(repository.GetAll().Where(d => d.Visible == true));
        }

        public ActionResult About()
        {
            return View();
        }

        public ActionResult SearchByTags(string query)
        {
            IDiplomProjectRepository projectRepository = repositoryService.DiplomProjectRepositoryCreateInstance();
            ITagRepository tagRepository = repositoryService.TagRepositoryCreateInstance();
            

            if (string.IsNullOrEmpty(query))
                return View(projectRepository.GetAll());
            List<DiplomProject> projects = new List<DiplomProject>();

            


            List<Tag> pool = tagRepository.GetAll().ToList();
         
            List<string> tags = ParseTags(query);

            foreach (string tag in tags)
            {
                foreach (Tag t in pool)
                {
                    if (t.Name.ToLower().Trim() == tag.ToLower().Trim())
                    {
                        foreach (DiplomProject p in t.Projects.ToList())
                        {
                            projects.Add(p);
                        }
                    }
                }
            }

            return View(projects);
        }

        private List<string> ParseTags(string query)
        {
            char[] separators = new char[] { ',', ';' };
            string[] field = query.Split(separators, StringSplitOptions.RemoveEmptyEntries);
            return field.ToList();
        }

        public ActionResult Culture(string id)
        {
            HttpCookie cookie = Request.Cookies["Culture"];
            cookie.Value = id;
            cookie.Expires = DateTime.Now.AddYears(1);
            Response.SetCookie(cookie);

            return RedirectToAction("Index","Home");
        }
    }
}
